package com.lyy.king.gateway.utils;

import com.alibaba.fastjson.JSONObject;
import com.lyy.king.common.core.domain.Result;
import org.springframework.http.HttpHeaders;
import org.springframework.http.HttpStatus;
import org.springframework.http.MediaType;
import org.springframework.http.server.reactive.ServerHttpResponse;
import org.springframework.web.server.ServerWebExchange;

/**
 * @BelongsProject: springcloud-learning
 * @BelongsPackage: com.cdx.gateway.utils
 * @Author: cdx
 * @CreateTime: 2022-06-05  20:07
 * @Description:
 * @Version: 1.0
 */
public class GeneralResponseUtils {
    /**
     * 错误信息返回
     * @param exchange
     * @param code
     * @param message
     * @return
     */
    public static ServerHttpResponse returnErrorResponse(ServerWebExchange exchange, Integer code, String message){
        ServerHttpResponse response = exchange.getResponse();
        response.setStatusCode(HttpStatus.OK);
        response.getHeaders().add(HttpHeaders.CONTENT_TYPE, MediaType.APPLICATION_JSON_VALUE);
        response.bufferFactory().wrap(JSONObject.toJSONBytes(Result.error(code,message)));
        return response;
    }

}
